<?php
namespace app\home\controller;
use app\home\model\ParentingModel;
use \think\Controller;

class Parenting extends Controller
{
    /**
     * 育儿首页
     */
    public function index(){
        $parentingType = new ParentingModel();
        $parentingType = $parentingType->parentingType();
        $parentingType = $this->showTreetwo($parentingType);

        $parentingInfo = new ParentingModel();
        $parentingInfo = $parentingInfo->parentingInfo();
        $parentingTypeId = $parentingInfo['knows_type_id'];

        $parentingTypeOne = new ParentingModel();
        $parentingTypeOne = $parentingTypeOne->parentingTypeOne($parentingTypeId);

        $parentingInfoTwo = new ParentingModel();
        $parentingInfoTwo = $parentingInfoTwo->parentingInfoTwo();
        $parentingInfoTwoId = $parentingInfoTwo[0]['knows_type_id'];

        $parentingTypeTwo = new ParentingModel();
        $parentingTypeTwo = $parentingTypeTwo->parentingTypeTwo($parentingInfoTwoId);

        $parentingInfoThree = new ParentingModel();
        $parentingInfoThree = $parentingInfoThree->parentingInfoThree();

        $this->assign('infoThree', $parentingInfoThree);
        $this->assign('infoTwo', $parentingInfoTwo);
        $this->assign('typeTwo', $parentingTypeTwo);
        $this->assign('typeOne', $parentingTypeOne);
        $this->assign('info', $parentingInfo);
        $this->assign('list', $parentingType);
        return $this->fetch('index');
    }

    /**
     * 育儿最新资讯
     */
    public function detailsInfo(){
        $id = input('id');
        $detailsInfo = new ParentingModel();
        $detailsInfo = $detailsInfo->detailsInfo($id);
        $parentingTypeOne = new ParentingModel();
        $parentingTypeOne = $parentingTypeOne->parentingTypeOne($detailsInfo['id']);
        $this->assign('detailsInfoType', $parentingTypeOne);
        $this->assign('detailsInfo', $detailsInfo);
        return $this->fetch('details_info');
    }

    /**
     * 育儿知识详情页
     */
    public function detailsPage(){
        $id = input('id');
        $detailsPage = new ParentingModel();
        $detailsPage = $detailsPage->detailsPage($id);
        $this->assign('detailsPage', $detailsPage);
        return $this->fetch('details');
    }

    /**
     * showTree无限极分类方法
     */
    public function showTreetwo($arr, $html = "&nbsp;&nbsp;&nbsp;&nbsp;", $pid = 0, $level = 0){
        $newArr = array();
        foreach($arr as $key => $v){
            if($v['parent_id'] == $pid) {
                $v['level'] = $level + 1;
                $v['html'] = str_repeat($html, $level) ;
                $v['data'] = $this->showTreetwo($arr, $html , $v['id'], $v['level']);
                $newArr[] = $v;
            }
        }
        return $newArr;
    }
}